package com.xinfu.newproject.model.login;

import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.login.SMSCodeBean;
import com.xinfu.newproject.bean.login.SMSCodeBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class SMSCodeModel implements IModel.ISMSCodeModel {

    @Override
    public void getSMSCode(SMSCodeBody smsCodeBody, final MyCallBack<SMSCodeBean> callBack) {
        Map<String, String> map = new HashMap<>();
        map.put("mobile", smsCodeBody.getUserName());
        map.put("smsmode", "1");
        JsNetworkService.getInstance().getIJsNetworkService()
                .getSMSCode(map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SMSCodeBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull SMSCodeBean smsCodeBean) {

                        callBack.onSuccess(smsCodeBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

}
